###################
### ENVIRONMENT ###
###################
import os
import sys

### LOAD GSLAB MAKE
ROOT = '..'
gslm_path = os.path.join(ROOT, 'lib', 'gslab_make')

sys.path.append(gslm_path)
import gslab_make as gs

### PULL PATHS FROM CONFIG
PATHS = {
    'root': ROOT,
    'config': os.path.join(ROOT, 'config.yaml')
}
PATHS = gs.update_internal_paths(PATHS)

### LOAD CONFIG USER 
PATHS = gs.update_external_paths(PATHS)
gs.update_executables(PATHS)

############
### MAKE ###
############

### START MAKE
gs.remove_dir(['input', 'external'])
gs.clear_dir(['output', 'log'])
gs.start_makelog(PATHS)

### INITIALIZE OUTPUT DIRECTORIES
os.makedirs('output/plots', exist_ok = True)
os.makedirs('output/scalars', exist_ok = True)
os.makedirs('output/tables', exist_ok = True)
os.makedirs('output/data_generated', exist_ok = True)

### MAKE LINKS TO INPUT AND EXTERNAL FILES
inputs = gs.link_inputs(PATHS, ['input.txt'])
externals = gs.link_externals(PATHS, ['external.txt'])
gs.write_source_logs(PATHS, inputs + externals)
#gs.get_modified_sources(PATHS, inputs + externals)

### RUN PROCESSING SCRIPTS
gs.run_r(PATHS, program = 'code/pull_estimates.R')

# ### RUN PLOTTING AND TABLE SCRIPTS
gs.run_r(PATHS, program = 'code/plot_sim.R')
gs.run_r(PATHS, program = 'code/plot_isobias.R')

### LOG OUTPUTS
gs.log_files_in_output(PATHS)

### CHECK FILE SIZES
#gs.check_module_size(PATHS)

### END MAKE
gs.end_makelog(PATHS)
